package com.ross.web.maintenance;

import java.io.IOException;
import java.util.List;

import javax.print.PrintException;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.ross.web.dao.CategoryDAO;
import com.ross.web.dao.ProductDAO;
import com.ross.web.domain.Category;
import com.ross.web.domain.Product;
import com.ross.web.util.PrintReceiptUtil;
import com.ross.web.util.SessionLogout;

public class ProductMaintenanceAction extends ActionSupport {
	
	private List<Product> productList;
	private ProductDAO productDAO = new ProductDAO();
	private Product product;
	private List<Category> categoryList;
	private Category category;
	private int dispMode;
	private String queryString;
	private int c_id;
	private HttpServletRequest request;
	
	CategoryDAO categoryDAO = new CategoryDAO();
	SessionLogout sessionLogout = new SessionLogout();
	public String goToProductMaintenance(){
		
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		categoryList = categoryDAO.getAllCategory();
		productList = productDAO.getAllProduct();
		setDispMode(1);
		
		request = ServletActionContext.getRequest();
		int i = productDAO.getAllProductOutofStock().size();
		
		if(i>0){
			request.getSession().setAttribute("warning", "show");
		}else{
			request.getSession().setAttribute("warning", "hide");
		}
		
		return SUCCESS;
	}
	
	public String addProductAction(){
		
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		if(!validateInputs()){
			categoryList = categoryDAO.getAllCategory();
			productList = productDAO.getAllProduct();
			setDispMode(1);
			return ERROR;
		}else{
			insert();
		}
		
		categoryList = categoryDAO.getAllCategory();
		productList = productDAO.getAllProduct();
		setProduct(null);
		setDispMode(1);
		
		return SUCCESS;
	}
	
	public String goToUpdateProductAction(){
		
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		categoryList = categoryDAO.getAllCategory();
		product = productDAO.getProductById(product.getProdId());
		setC_id(product.getCategory().getC_id());
		setDispMode(2);
		return SUCCESS;
	}
	
	public String updateProductAction(){
		
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		if(!validateInputs()){
			categoryList = categoryDAO.getAllCategory();
			setDispMode(2);
			return ERROR;
		}else{
			update();
		}
		
		categoryList = categoryDAO.getAllCategory();
		productList = productDAO.getAllProduct();
		setProduct(null);
		setDispMode(1);
		
		return SUCCESS;
	}
	
	public String deleteProductAction(){
		
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		product = productDAO.getProductById(product.getProdId());
		productDAO.deleteProduct(product);
		addActionMessage(getText("success.productmaintenance.delete"));
		
		categoryList = categoryDAO.getAllCategory();
		productList = productDAO.getAllProduct();
		setProduct(null);
		setDispMode(1);
		
		return SUCCESS;
		
	}
	
	public String productMaintenanceSearch(){
		
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		categoryList = categoryDAO.getAllCategory();
		productList = productDAO.getProductByName(queryString);
		setDispMode(1);
		
		addActionMessage("Query returned "+productList.size()+" results");
		
		return SUCCESS;
		
	}
	
	private boolean validateInputs(){
		
		
		if(product.getBarcode().trim().length() == 0){
			addActionError(getText("error.productmaintenance.barcoderequired"));
		}else{
			if(getDispMode() == 1){
				productDAO = new ProductDAO();
				Product prod = new Product();
				prod = productDAO.getProductByBarCode(product.getBarcode());
				if(prod != null){
					addActionError(getText("error.productmaintenance.productexist"));
				}
			}
		}
		
		if(product.getName().trim().length() == 0){
			addActionError(getText("error.productmaintenance.namerequired"));
		}else if(product.getName().length() >= 16){
			addActionError(getText("error.productmaintenance.exceedname"));
		}
		
		if(product.getInformation().trim().length() == 0){
			addActionError(getText("error.productmaintenance.inforequired"));
		}else if(product.getInformation().length() >= 200){
			addActionError(getText("error.productmaintenance.exceedinfo"));
		}
		
		if(product.getPrice().trim().length() == 0){
			addActionError(getText("error.productmaintenance.pricerequired"));
		}else{
			
			try{
				Double.valueOf(product.getPrice());
			}catch (Exception e) {
				addActionError(getText("error.productmaintenance.invalidprice"));
			}
		}
		
		if(getC_id() == 0){
			addActionError(getText("error.productmaintenance.categoryrequired"));
		}
		
		if(this.hasActionErrors()){
			return false;
		}else{
			return true;
		}
	}
	
	private void insert(){
		String name = product.getName();
		name = name.substring(0,1).toUpperCase() + name.substring(1);
		Category c = categoryDAO.getAllCategoryById(getC_id());
		product.setC_id(c.getC_id());
		product.setCategory(c);
		product.setName(name);
		productDAO.insertProduct(product);
		
		addActionMessage(getText("success.productmaintenance.insert"));
	}
	
	private void update(){
		String name = product.getName();
		name = name.substring(0,1).toUpperCase() + name.substring(1);
		Category c = categoryDAO.getAllCategoryById(getC_id());
		product.setC_id(c.getC_id());
		product.setCategory(c);
		product.setName(name);
		productDAO.editProduct(product);
		addActionMessage(getText("success.productmaintenance.update"));
	}
	
	
	

	public int getC_id() {
		return c_id;
	}

	public void setC_id(int c_id) {
		this.c_id = c_id;
	}

	public List<Category> getCategoryList() {
		return categoryList;
	}

	public void setCategoryList(List<Category> categoryList) {
		this.categoryList = categoryList;
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public String getQueryString() {
		return queryString;
	}

	public void setQueryString(String queryString) {
		this.queryString = queryString;
	}

	public int getDispMode() {
		return dispMode;
	}

	public void setDispMode(int dispMode) {
		this.dispMode = dispMode;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public List<Product> getProductList() {
		return productList;
	}

	public void setProductList(List<Product> productList) {
		this.productList = productList;
	}
	
	
	
}
